<template>
  <div class="col-row-center" style="width: 100%;height: 100%">
    <div v-if="!hasPartner || hasCode" class="col-row-center">
      <div v-if="needOpenNextPage" style="font-size: 20px;font-weight: bold;margin-top: 80px">
        请稍作等待{{ countNum }}s，正在跳转中....
      </div>
      <img style="height: 300px;width: 300px;margin-bottom:20px;margin-top: 50px"
           src="https://cdxapp-1257733245.cos.ap-beijing.myqcloud.com/peiwan/wanwei/bjwanwei.jpg"/>
      <div style="font-size: 16px;font-weight: bold;">
        有任何问题可添加客服微信咨询
      </div>
    </div>
    <div v-else style="font-size: 20px">欢迎访问玩味科技</div>
  </div>
</template>

<script lang="ts">
import {Options, Vue} from 'vue-property-decorator'
//@ts-ignore
import request from "@/plugins/request";

@Options({})
export default class HomeView extends Vue {

  countNum = 3
  setTime = null


  get hasCode() {
    const code = this.$route.query?.code
    return !!code
  }

  get code() {
    const code = this.$route.query?.code
    return code
  }

  get hasPartner() {
    const hostName = window.location.hostname
    //如果为邀请页面，则必然跳转不能停留
    return hostName.includes('partner.socialuni.cn')
  }

  created() {
    if (this.hasCode) {
      request.get('peiwan/code?code=' + this.code)
    } else {
      request.get('peiwan/code')
    }
    this.setTime = setInterval(() => {
      if (this.countNum > 1) {
        this.countNum--
        if (this.countNum === 1) {
          if (this.setTime) {
            clearInterval(this.setTime)
            this.setTime = null
          }
        }
      }
    }, 500)
    this.openNextPage()
  }

  mounted() {
    this.openNextPage()
  }

  updated() {
    this.openNextPage()
  }

  get needOpenNextPage() {
    return this.hasPartner || this.hasCode
  }

  openNextPage() {
    //如果为邀请页面，则必然跳转不能停留
    if (this.needOpenNextPage) {
      if (this.hasCode) {
        location.href = `https://gitee.com/socialuni/peiwan/blob/master/partner/partner${this.code}.md`
      } else {
        location.href = 'https://gitee.com/socialuni/peiwan/blob/master/readme.md'
      }
    }
  }
}
</script>
<style>
.col-row-center {
  display: flex;
  flex-direction: column;
  align-items: center;
}
</style>
